<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }
        .btn:hover {
            background-color: #45a049;
        }
        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>欢迎登录</h2>
        <div id="message" class="message"></div>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <label for="captchaInput">验证码:</label>
                <div style="display: flex; align-items: center;">
                    <input type="text" id="captchaInput" name="captchaInput" required style="flex: 1; margin-right: 10px;">
                    <img id="captchaImage" src="" alt="验证码" style="cursor: pointer; height: 38px;">
                </div>
                <input type="hidden" id="captchaId" name="captchaId">
            </div>
            <button type="submit" class="btn">登录</button>
        </form>
    </div>

    <script>
        // 获取验证码
        function loadCaptcha() {
            // 添加时间戳防止浏览器缓存
            const timestamp = new Date().getTime();
            fetch('/api/captcha/generate?t=' + timestamp)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('captchaImage').src = data.image;
                    document.getElementById('captchaId').value = data.captchaId;
                })
                .catch(error => {
                    console.error('获取验证码失败:', error);
                });
        }
        
        // 页面加载时获取验证码
        window.onload = function() {
            loadCaptcha();
        };
        
        // 点击验证码图片刷新验证码
        document.getElementById('captchaImage').addEventListener('click', function() {
            loadCaptcha();
        });
        
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const captchaId = document.getElementById('captchaId').value;
            const captchaInput = document.getElementById('captchaInput').value;
            const messageDiv = document.getElementById('message');
            
            fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password, captchaId, captchaInput })
            })
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    return response.text().then(text => {
                        throw new Error(text);
                    });
                }
            })
            .then(data => {
                // 存储token
                localStorage.setItem('token', data.token);
                localStorage.setItem('username', data.username);
                localStorage.setItem('role', data.role);
                
                // 显示成功消息
                messageDiv.textContent = '登录成功！正在跳转...';
                messageDiv.className = 'message success';
                messageDiv.style.display = 'block';
                
                // 跳转到主页
                setTimeout(() => {
                    window.location.href = '/index.html';
                }, 1500);
            })
            .catch(error => {
                // 显示错误消息
                messageDiv.textContent = error.message;
                messageDiv.className = 'message error';
                messageDiv.style.display = 'block';
                
                // 刷新验证码
                loadCaptcha();
                document.getElementById('captchaInput').value = '';
            });
        });
    </script>
</body>
</html>